//
//  RGSearchSlideView.m
//  RedGrass
//
//  Created by wukong on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSearchSlideView.h"

@interface RGSearchSlideView()

@property (nonatomic, strong) UIView                           *slideLightView;
@property (nonatomic, strong) NSMutableArray<UILabel *>        *labels;
@property (nonatomic, strong) NSMutableArray<NSString *>       *titles;
@property (nonatomic, assign) NSInteger                        tabIndex;
@property (nonatomic, assign) CGFloat                          itemWidth;


@end

@implementation RGSearchSlideView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self){
        _labels = [NSMutableArray array];
        _titles = [NSMutableArray array];
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    if(_titles.count == 0) {
        return;
    }
    
    [[self subviews] enumerateObjectsUsingBlock:^(UIView *subView, NSUInteger idx, BOOL *stop) {
        [subView removeFromSuperview];
    }];
    [_labels removeAllObjects];
    
    CGFloat itemWidth = _itemWidth = 70;
    [_titles enumerateObjectsUsingBlock:^(NSString * title, NSUInteger idx, BOOL *stop) {
        UIView *container = [[UIView alloc]init];
        container.userInteractionEnabled = YES;
        container.tag = idx;
        [container addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapAction:)]];
        container.frame = CGRectMake(idx*itemWidth, 0, itemWidth, self.bounds.size.height);
        [self addSubview:container];
        UILabel *label = [[UILabel alloc]init];
        label.text = title;
        label.textColor = UICOLOR_WITH_RGBINT_A(0xffffff, 0.6);
        //        label.textAlignment = NSTextAlignmentCenter;
        label.font = FONT(customFontName, 17);
        
        //        label.userInteractionEnabled = YES;
        //        [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTapAction:)]];
        [container addSubview:label];
        [self.labels addObject:label];
        [container addSubview:label];
        label.frame = CGRectMake(20, 0, itemWidth - 20, self.bounds.size.height);
        //        label.frame = CGRectMake(idx*itemWidth, 0, itemWidth, self.bounds.size.height);
        //        if(idx != self.titles.count - 1) {
        //            UIView *spliteLine = [[UIView alloc] initWithFrame:CGRectMake((idx+1)*itemWidth - 0.25f, 12.5f, 0.5f, self.bounds.size.height - 25.0f)];
        //            spliteLine.backgroundColor = UICOLOR_WITH_RGBINT_A(0xffffff, 0.2);
        //            spliteLine.layer.zPosition = 10;
        //            [self addSubview:spliteLine];
        //        }
    }];
    _labels[_tabIndex].textColor = [UIColor whiteColor];
  
    self.slideLightView.frame = CGRectMake(_tabIndex * itemWidth + 10, self.bounds.size.height - 6, _itemWidth - 20, 4);
    [self addSubview:self.slideLightView];
    //    _slideLightView = [[UIView alloc] init];
    //    _slideLightView.backgroundColor = [UIColor redColor];
    //    _slideLightView.frame = CGRectMake(_tabIndex * itemWidth + 15, self.bounds.size.height-2, itemWidth - 30, 2);
    //    [self addSubview:_slideLightView];
}

- (void)setLabels:(NSArray<NSString *> *)titles tabIndex:(NSInteger)tabIndex {
    [_titles removeAllObjects];
    [_titles addObjectsFromArray:titles];
    _tabIndex = tabIndex;
}

- (void)onTapAction:(UITapGestureRecognizer *)sender {
    NSInteger index = sender.view.tag;
    if(_delegate) {
        [UIView animateWithDuration:0.10
                              delay:0
             usingSpringWithDamping:0.8
              initialSpringVelocity:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             CGRect frame = self.slideLightView.frame;
                             frame.origin.x = self.itemWidth * index + 10;
                             [self.slideLightView setFrame:frame];
                             [self.labels enumerateObjectsUsingBlock:^(UILabel *label, NSUInteger idx, BOOL *stop) {
                                 label.textColor = index == idx ? [UIColor whiteColor] : UICOLOR_WITH_RGBINT_A(0xffffff, 0.6);
                             }];
                         } completion:^(BOOL finished) {
                             [self.delegate onTabTapAction:index];
                         }];
        
    }
}

-(UIView *)slideLightView{
    if(!_slideLightView){
        _slideLightView = [[UIView alloc] init];
        _slideLightView.backgroundColor = [UIColor redColor];
        ViewRadius(_slideLightView, 2);
        //        _slideLightView.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.5);
        //        _slideLightView.frame = CGRectMake(_tabIndex * itemWidth + 15, self.bounds.size.height-2, itemWidth - 30, 2);
    }
    return _slideLightView;
}


@end
